package com.itrident.dialog;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.TabbedPaneImpl;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.table.JBTable;
import com.itrident.handler.RecodeEnumsHandler;
import com.itrident.store.EnumsInfo;
import com.itrident.store.EnumsWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @author cxx
 * @create 2021-04-19 16:31
 **/
public class EnumsManagerDialog extends DialogWrapper{

    private Project project;

    private String oldCode;

    private String oldDesc;

    private JTextField textFieldCode = null;

    private JTextField textFieldDesc =null;

    public EnumsManagerDialog(@Nullable Project project) {
        super (project);
        this.project = project;
        init();
        this.setTitle ("记录枚举管理");
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {

        JPanel dialogPanel = new JBPanel(new BorderLayout ());
        JTabbedPane jTabbedPane = new TabbedPaneImpl (SwingConstants.LEFT);
        jTabbedPane.setPreferredSize (new Dimension (650,350));
        dialogPanel.setPreferredSize (new Dimension (650,350));
        JBPanel setContent = new JBPanel (new BorderLayout());
        JBPanel textContent = new JBPanel (new GridLayout (0,3,0,0));
        setContent.add (textContent,BorderLayout.CENTER);
        JPanel content2 = new JBPanel ();
        JBTable jbTable = new JBTable ();
        String[] colName ={"类名","值","描述"};
        String[][] datas = initData();
        TableModel tableModel = new DefaultTableModel (datas,colName){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jbTable.setModel (tableModel);
        JBScrollPane jbScrollPane = new JBScrollPane (jbTable);
        jbScrollPane.setPreferredSize (new Dimension (600,350));
        content2.add (jbScrollPane);
        jTabbedPane.addTab ("设置",setContent);
        jTabbedPane.addTab ("数据",content2);
        JLabel jLabel1 = new JLabel ("枚举取值：");
        this.oldCode = RecodeEnumsHandler.getCode (project);
        textFieldCode = new JBTextField (this.oldCode,10);
        textFieldCode.setPreferredSize (new Dimension (100,40));
        JLabel jLabel2 = new JLabel ("枚举描述：");
        this.oldDesc = RecodeEnumsHandler.getDesc (project);
        textFieldDesc = new JBTextField (this.oldDesc,10);
        textFieldDesc.setPreferredSize (new Dimension (100,40));
        JBPanel<JBPanel> jbPanel1 = new JBPanel<> (new FlowLayout (FlowLayout.LEFT));
        jbPanel1.add (jLabel1);
        jbPanel1.add (textFieldCode);

        JBPanel<JBPanel> jbPanel2 = new JBPanel<> (new FlowLayout (FlowLayout.LEFT));
        jbPanel2.add (jLabel2);
        jbPanel2.add (textFieldDesc);
        textContent.add (jbPanel1);
        textContent.add (jbPanel2);
        dialogPanel.add (jTabbedPane,BorderLayout.WEST);
        JPanel jPanelSouth = new JBPanel (new BorderLayout());
        JButton clearBut = new JButton ("清空记录");
        clearBut.addActionListener (e -> {
            RecodeEnumsHandler.delAllRecodeEnums(project);
        });
        JButton reloadBut = new JButton ("重新载入");
        reloadBut.addActionListener (e->{
            RecodeEnumsHandler.reloadRecodeEnums (project);
        });
        jPanelSouth.add (clearBut,BorderLayout.EAST);
        jPanelSouth.add (reloadBut,BorderLayout.WEST);
        setContent.add (jPanelSouth,BorderLayout.SOUTH);
        return dialogPanel;
    }

    private String[][] initData() {
        List<EnumsWrapper> enumsWrappers = RecodeEnumsHandler.getAllRecodeEnums(project);
        List<String[]> arrs = new ArrayList<> ();
        String[] sarr = null;
        for (EnumsWrapper enumsWrapper : enumsWrappers) {
            List<EnumsInfo> enumsInfos = enumsWrapper.getEnumsInfos ();
            for (EnumsInfo enumsInfo : enumsInfos) {
                sarr = new String[3];
                sarr[0] = enumsWrapper.getClassName ();
                sarr[1] = enumsInfo.getCode ();
                sarr[2] = enumsInfo.getDesc ();
                arrs.add (sarr);
            }
        }
        String[][] arr = new String[arrs.size ()][3];
        return arrs.toArray (arr);
    }

    @Override
    protected void doOKAction() {
        if(textFieldCode != null && textFieldDesc != null){
            String codeText = textFieldCode.getText ();
            String descText = textFieldDesc.getText ();
            if (codeText.equals (oldCode) && textFieldDesc.getText ().equals (oldDesc)) {
                super.doOKAction ();
                return;
            }else{
                RecodeEnumsHandler.setCode (project,codeText);
                RecodeEnumsHandler.setDesc (project,descText);
                RecodeEnumsHandler.reloadRecodeEnums (project);
            }
        }
        super.doOKAction ();
    }
}
